package com.issue.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.issue.common.core.domain.entity.SysUser;
import com.issue.system.domain.AChildInfo;
import com.issue.system.mapper.AChildInfoMapper;
import com.issue.system.mapper.ChildAttendanceMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.enums.BusinessType;
import com.issue.system.domain.ChildAttendance;
import com.issue.system.service.IChildAttendanceService;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.common.core.page.TableDataInfo;

/**
 * AttendanceController
 * 
 * @author system
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/system/attendance")
public class ChildAttendanceController extends BaseController
{
    @Autowired
    private IChildAttendanceService childAttendanceService;

    @Resource
    private AChildInfoMapper childInfoMapper;

    @Resource
    private ChildAttendanceMapper childAttendanceMapper;

    /**
     * 查询Attendance列表
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildAttendance childAttendance)
    {
        startPage();
        List<ChildAttendance> list = childAttendanceService.selectChildAttendanceList(childAttendance);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:attendance:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(@RequestParam("phone") String phone)
    {
        List<ChildAttendance> list = childAttendanceMapper.selectListByPhone(phone);
        return getDataTable(list);
    }

    /**
     * 导出Attendance列表
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:export')")
    @Log(title = "Attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildAttendance childAttendance)
    {
        List<ChildAttendance> list = childAttendanceService.selectChildAttendanceList(childAttendance);
        ExcelUtil<ChildAttendance> util = new ExcelUtil<ChildAttendance>(ChildAttendance.class);
        util.exportExcel(response, list, "Attendance数据");
    }

    /**
     * 获取Attendance详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(childAttendanceService.selectChildAttendanceById(id));
    }

    /**
     * 新增Attendance
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:add')")
    @Log(title = "Attendance", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildAttendance childAttendance)
    {
        Long childId = childAttendance.getChildId();

        if (childId == null) {
            return AjaxResult.error("Error Message");
        }

        AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
        if (childInfo == null){
            return AjaxResult.error("Error Child");
        }
        childAttendance.setChildCardNo(childInfo.getChildCardNo());
        childAttendance.setChildName(childInfo.getChildName());

        return toAjax(childAttendanceService.insertChildAttendance(childAttendance));
    }

    /**
     * 修改Attendance
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:edit')")
    @Log(title = "Attendance", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildAttendance childAttendance)
    {

        Long childId = childAttendance.getChildId();
        if (childId != null) {
            AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
            if (childInfo != null){
                childAttendance.setChildCardNo(childInfo.getChildCardNo());
                childAttendance.setChildName(childInfo.getChildName());
            }
        }
        return toAjax(childAttendanceService.updateChildAttendance(childAttendance));
    }

    /**
     * 删除Attendance
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:remove')")
    @Log(title = "Attendance", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(childAttendanceService.deleteChildAttendanceByIds(ids));
    }
}
